package util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import play.Logger;

public class PlausabilityCalculator {

    protected static org.apache.log4j.Logger validationJobLogger = org.apache.log4j.Logger.getLogger("validationJob");

    private static final int MAX_POSSIBILITY = 100;
    
    /**
     * Are the amount of elements in the list equal to the 
     * @param plausabilityList
     * @param expectedAmountOfValues
     * @return
     */
    public static int calcPlausibilityElementsInList(ArrayList<Integer> plausabilityList, int expectedAmountOfValues) {
        if (plausabilityList.size() == 0) {
            validationJobLogger.warn("AverageList is empty, correctness is 0");
            return 0;
        } else {
            int actualAmountOfValues = plausabilityList.size();
            validationJobLogger.debug("ExpectedAmountOfValues: " + expectedAmountOfValues + " ActualAmountOfValues: "
                    + actualAmountOfValues);
            try {
                int plausability = (int) (((double) actualAmountOfValues / (double) expectedAmountOfValues) * 100);
                Logger.debug("calcPlausibilityElementsInList: %s", plausability);
                if (plausability > MAX_POSSIBILITY) {
                    return MAX_POSSIBILITY;
                }
                return plausability;
            } catch (Exception e) {
                Logger.error(e, "CalcPlausibility failed, set plausibility to 0");
                return 0;
            }
        }
    }

    /**
     * Calculates the plausibility for values >= 1 or >=100
     * don't mix the type!  
     * @param possibilities
     * @return the multiplied possibilities
     */
    public static int consolidatePossibilities(double[] possibilities) {
        if (possibilities.length == 0) {
            return 0;
        }
        double plausa = 1.0;
        for (int i = 0; i < possibilities.length; i++) {
            double factor = possibilities[i];
            // if someone mixes 100.0 and 1.0 for 100% -->  Shit happens!
            if (factor <= 1.0) {
                plausa *= factor;
            } else {
                plausa *= (factor / 100);
            }
        }
        return (int) (plausa*100);
    }

    /**
     * Calculates the plausibility for values >= 1 or >=100
     * don't mix the type!  
     * @param possibilities
     * @return the multiplied possibilities
     */
    public static int consolidatePossibilities(double d1, double d2) {
        return consolidatePossibilities(new double[] { d1, d2 });
    }

}
